/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.register;

import java.util.HashMap;

import com.alstudio.module.xmpp.protocol.xmpp.data.ALXmppData;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALXmppDomainConfig;
import com.alstudio.module.xmpp.protocol.xmpp.packact.IQNameSpace;
import com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.AlXmlTag;
import com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.BaseIQTemplate;
import com.alstudio.utils.encode.ALDES;

/**
 * @author alonso lee
 * 
 */
public class CheckThirdPartLoginStateIQCreator extends BaseIQTemplate {

	private ALXmppData data;

	/**
	 * 
	 */
	public CheckThirdPartLoginStateIQCreator(ALXmppData data) {
		// TODO Auto-generated constructor stub
		this.data = data;
		this.domain = data.getDomain();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.util.xmlcreator.iq.BaseIQTemplate
	 * #getIQXml()
	 */
	@Override
	public String getIQXml() {
		// TODO Auto-generated method stub
		to = ALXmppDomainConfig.getALXmppDomainConfig().getDomainByNameSpace(
				IQNameSpace.NS_THIRD_PART_LOGIN);
		mALIQCreator.setTo(to + domain);
		mALIQCreator.setNs(IQNameSpace.NS_THIRD_PART_LOGIN);
		mALIQCreator.setType(IQ_TYPE_GET);

		String thirdPartType = null;
		switch (data.getThirdPartType()) {
		case ALXmppData.LOGIN_TYPE_FACEBOOK:
			thirdPartType = "facebook";
			break;
		case ALXmppData.LOGIN_TYPE_TWITTER:
			thirdPartType = "twitter";
			break;
		case ALXmppData.LOGIN_TYPE_SINA:
			thirdPartType = "sina";
			break;
		case ALXmppData.LOGIN_TYPE_TENCENT:
			thirdPartType = "qq";
			break;
		}

		AlXmlTag typeTag = new AlXmlTag(thirdPartType);
		HashMap<String, String> typeTagAtts = new HashMap<String, String>();
		try {
			typeTagAtts.put("accesstoken", ALDES.encryptDES(
					data.getThirdPartUid(), ALXmppData.DES_KEY));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		typeTag.setAtts(typeTagAtts);

		AlXmlTag versionTag = new AlXmlTag("version");
		versionTag.setText(data.getAppVersion());

		AlXmlTag deviceTag = new AlXmlTag("uniqueid");
		try {
			deviceTag.setText(ALDES.encryptDES(data.getDevInfo().getIMEI(),
					ALXmppData.DES_KEY));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		tagList.add(typeTag);
		tagList.add(versionTag);
		tagList.add(deviceTag);
		mALIQCreator.setTagList(tagList);
		String xml = "";
		try {
			xml = mALIQCreator.getXml();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return xml;
	}

}
